﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.Security;
using ZDSoft.UPMS.Model.SystemPart;
using ZDSoft.UPMS.Web.App;
using ZDSoft.UPMS.Web.Models;
using ZDSoft.UPMS.Core;
using ZDSoft.UPMS.Service.SystemPart;
using NHibernate;
using NHibernate.Criterion;


namespace ZDSoft.UPMS.Web.Controllers
{
    public class AccountController : Controller
    {

        public ActionResult Login()
        {
            return View();
        }
        public string AJAXLogin(string account, string password, string checkCode)
        {
            ViewData["account"] = account;
            System.Threading.Thread.Sleep(1000);
            password = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(password, "md5");
            User user = Container.Instance.Resolve<IUserService>().Login(account, password);
            if (user != null)
            {
                if (checkCode == Session["CheckCode"].ToString())
                {
                    if (user.IsReset == true)
                    {
                        return "重置密码已提交，管理员将尽快处理！";
                    }
                    else
                    {
                        AppHelper.LoginedUser = user;
                        return "success";
                    }
                }
                else
                {
                    return "验证码错误！";
                }
            }
            else
            {
                return "用户名或密码错误或该用户名暂未激活！";
            }
        }

        

        #region 修改密码
        [HttpGet]
        public ActionResult ChangePassword()
        {
            return View();
        }

        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="oldpwd">旧密码</param>
        /// <param name="newpwd">新密码</param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult ChangePassword(string oldpwd, string newpwd)
        {
            oldpwd = AppHelper.EncodeMd5(oldpwd);
            User user = Container.Instance.Resolve<IUserService>().FindByID(AppHelper.LoginedUser.ID);
            if (oldpwd == user.Password)
            {
                user.Password = AppHelper.EncodeMd5(newpwd);
                Container.Instance.Resolve<IUserService>().Update(user);
            }
            return View();
        }

        /// <summary>
        /// 检测原始密码
        /// </summary>
        /// <param name="oldpwd">原始密码</param>
        /// <returns></returns>
        public ActionResult CheckOldPassword(string oldpwd)
        {
            oldpwd = AppHelper.EncodeMd5(oldpwd);
            User user = Container.Instance.Resolve<IUserService>().FindByID(AppHelper.LoginedUser.ID);
            if (oldpwd == user.Password)
            {
                return Json("ok", JsonRequestBehavior.AllowGet);
            }
            else
            {
                return Json("", JsonRequestBehavior.AllowGet);
            }

        }

        #endregion

        public ActionResult Resetpassw()
        {
            return View();
        }

        public string ResetPassword(string Account)
        {

            List<ICriterion> cris = new List<ICriterion>();
            cris.Add(new LikeExpression("Account", Account));
            IList<User> us = Core.Container.Instance.Resolve<IUserService>().Find(cris);
            foreach (User u in us)
            {
                u.IsReset = true;
                Core.Container.Instance.Resolve<IUserService>().Update(u);
            }    
                return "1";         
        }
    }
}
